package com.tzxs.pay.main.bill;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.Toast;

import com.tzxs.pay.BR;
import com.tzxs.pay.R;
import com.tzxs.pay.databinding.FragmentAuthorizationBinding;
import com.tzxs.pay.databinding.ItemBillAuthorizationBinding;
import com.tzxs.pay.main.BaseFragment;
import com.tzxs.pay.main.bill.detail.AutTncActivity;
import com.tzxs.pay.main.bill.detail.AutTradedDetailActivity;
import com.tzxs.pay.main.bill.model.AutBillImp;
import com.tzxs.pay.main.bill.model.AutOrderBean;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.util.IAdapter;
import com.tzxs.pay.util.IHolder;

import java.util.List;


public class AuthorizationFragment extends BaseFragment<FragmentAuthorizationBinding> implements IAdapter.ItemNavigator<AutBillImp> {
    private static final String TAG = "AuthorizationFragment";

    private BillViewModel model;
    private int page = 1;//默认第一页
    private int pageNum = 10;//每页10

    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_authorization;
    }

    @Override
    protected void viewCreate(Bundle savedInstanceState) {
        //初始化数据
        binding.billAuthRec.setLayoutManager(new LinearLayoutManager(getContext()));
        //初始化viewModel
        model = ViewModelProviders.of(this).get(BillViewModel.class);

        binding.setViewModel(model);
        //绑定viewModel
        binding.billAuthRec.setAdapter(new IAdapter<AutBillImp, ViewDataBinding>() {
            @Override
            protected void convertListener(@NonNull ViewDataBinding binding) {
                super.convertListener(binding);

                if (binding instanceof ItemBillAuthorizationBinding) {
                    ItemBillAuthorizationBinding dataBinding = (ItemBillAuthorizationBinding) binding;
                    dataBinding.setItemNavigator(AuthorizationFragment.this);
                }

            }

            @Override
            protected int getLayoutId(int viewType) {

                return viewType;
            }


            @Override
            public void onBindViewHolder(IHolder<ViewDataBinding> holder, int position) {
                super.onBindViewHolder(holder, position);
                if (getData().get(position) instanceof AutOrderBean.DataBean) {
                    AutOrderBean.DataBean bean = (AutOrderBean.DataBean) getData().get(position);
                    if (holder.binding instanceof ItemBillAuthorizationBinding) {
                        ItemBillAuthorizationBinding dataBinding = (ItemBillAuthorizationBinding) holder.binding;
                        String payStr = null;
                        //订单状态
                        switch (bean.getPay_status()) {
                            case 1:
                                payStr = "支付成功(待结算)";
                                dataBinding.autState.setTextColor(Color.parseColor("#1F78CF"));
                                break;
                            case 2:
                                payStr = "支付失败";
                                dataBinding.autState.setTextColor(Color.parseColor("#999999"));
                                break;
                            case 3:
                                payStr = "等待支付";
                                dataBinding.autState.setTextColor(Color.parseColor("#27AA30"));
                                break;
                            case 4:
                                payStr = "结算成功";
                                dataBinding.autState.setTextColor(Color.parseColor("#ED433F"));
                                break;
                            case 5:
                                payStr = "已退款";
                                dataBinding.autState.setTextColor(Color.parseColor("#EB9205"));
                                break;

                        }
                        dataBinding.autState.setText(payStr);

                        //支付方式
                        switch (bean.getPay_type()) {
                            case "weixin":
                                dataBinding.autPayIcon.setImageResource(R.drawable.ic_wechat);
                                break;
                            case "alipay":
                                dataBinding.autPayIcon.setImageResource(R.drawable.ic_alipay);
                                break;
                            default:
                                dataBinding.autPayIcon.setImageResource(R.drawable.ic_unionpay);
                                break;
                        }
                    }

                }
            }

            @Override
            protected int getDataBRId(int itemViewType) {
                //其实你的xml里面的BR.这个ID可以用同一个的,但是你区分就保留
                if (itemViewType == R.layout.item_atu_header) {
                    return BR.head;
                }
                return BR.order_aut;
            }

            @Override
            public boolean areItemsTheSame(List<AutBillImp> oldData, List<AutBillImp> data, int oldItemPosition, int newItemPosition) {
                if (oldData.get(oldItemPosition) instanceof AutOrderBean.DataBean && data.get(newItemPosition) instanceof AutOrderBean.DataBean) {
                    return ((AutOrderBean.DataBean) oldData.get(oldItemPosition)).getOut_order_no().equals(((AutOrderBean.DataBean) data.get(newItemPosition)).getOut_order_no());
                } else
                    return super.areItemsTheSame(oldData, data, oldItemPosition, newItemPosition);
            }

            //根据position返回type
            @Override
            public int getItemViewType(int position) {
                if (position == 0) {
                    return R.layout.item_atu_header;
                } else
                    return R.layout.item_bill_authorization;

            }
        });

        getData();//进入后第一次获取数据


        //刷新
        binding.refresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            //获取数据
            model.getDepositBills(true, null, page, pageNum, null, null, null,
                    null, 0, null, () -> refreshLayout.finishRefresh(2000));

        });
        //加载更多
        binding.refresh.setOnLoadMoreListener(refreshLayout ->

        {
            ++page;
            model.getDepositBills(true, null, page, pageNum, null, null, null,
                    null, 0, null, () -> refreshLayout.finishLoadMore(2000));


        });

        //观察提示
        model.snackbarText.observe(this, s -> Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show());

    }


    private void getData() {
        //获取数据
        model.getDepositBills(true, null, page, 10, null, null, null,
                null, 0, null, () -> Log.d(TAG, "success: 获取数据成功！"));

    }

    /**
     * 点击事件
     *
     * @param item ItemView用到的数据
     */
    @Override
    public void itemClick(AutBillImp item) {

        if (item instanceof AutOrderBean.DataBean) {
            AutOrderBean.DataBean dataBean = (AutOrderBean.DataBean) item;
            Intent intent;
            if (dataBean.getPay_status() == 1) {//订单未结算的情况下跳转
                intent = new Intent(getActivity(), AutTncActivity.class);

            } else {
                intent = new Intent(getActivity(), AutTradedDetailActivity.class);
            }
            intent.putExtra("orderId", dataBean.getOut_order_no());
            startActivity(intent);
        }


    }
}
